package sealte.hvpms.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sealte.hvpms.dao.impl.MessageDAOImpl;
import sealte.hvpms.pojo.Admin;
import sealte.hvpms.pojo.Message;

public class AdminModifyMessage extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		Admin admin = (Admin)request.getSession().getAttribute("admin");
		PrintWriter out = response.getWriter();
		if(admin!=null){
			String messageTitle = (String)request.getParameter("messageTitle");
			String messageContent = (String)request.getParameter("messageContent");
			String messageID = (String)request.getParameter("id");
			int id = Integer.parseInt(messageID);
			String message = "";
			MessageDAOImpl mb = new MessageDAOImpl();
			Message mes = new Message();
			mes.setTitle(messageTitle);
			mes.setContent(messageContent);
			mes.setId(id);
			boolean flag = mb.modifyMessage(mes);
			if(flag){
				message = "修改成功！";
			}else{
				message = "修改失败！";
			}
			request.setAttribute("message_modify", message);
			request.getRequestDispatcher("hvpms_admin/messages_view.jsp").forward(request, response);
		}else{
			out.println("谢绝跨路径访问！");
		}
	}

}
